package com.ly.dingtalk.service.dept.impl;

import static com.ly.dingtalk.constant.DingUrlConstant.Department.GET_DEPT_DETAIL;
import static com.ly.dingtalk.constant.DingUrlConstant.Department.GET_PARENT_DEPT_LIST_BY_DEPT_ID;
import static com.ly.dingtalk.constant.DingUrlConstant.Department.GET_PARENT_DEPT_LIST_BY_USER_ID;
import static com.ly.dingtalk.constant.DingUrlConstant.Department.GET_SUB_DEPT_ID_LIST;
import static com.ly.dingtalk.constant.DingUrlConstant.Department.GET_SUB_DEPT_LIST;

import java.util.List;

import org.apache.commons.lang3.ObjectUtils;
import org.jetbrains.annotations.NotNull;

import com.aliyun.dingtalkcontact_1_0.models.SearchDepartmentHeaders;
import com.aliyun.dingtalkcontact_1_0.models.SearchDepartmentRequest;
import com.aliyun.dingtalkcontact_1_0.models.SearchDepartmentResponse;
import com.aliyun.teautil.models.RuntimeOptions;
import com.dingtalk.api.request.OapiV2DepartmentGetRequest;
import com.dingtalk.api.request.OapiV2DepartmentListparentbydeptRequest;
import com.dingtalk.api.request.OapiV2DepartmentListparentbyuserRequest;
import com.dingtalk.api.request.OapiV2DepartmentListsubRequest;
import com.dingtalk.api.request.OapiV2DepartmentListsubidRequest;
import com.dingtalk.api.response.OapiV2DepartmentGetResponse;
import com.dingtalk.api.response.OapiV2DepartmentListparentbydeptResponse;
import com.dingtalk.api.response.OapiV2DepartmentListparentbyuserResponse;
import com.dingtalk.api.response.OapiV2DepartmentListsubResponse;
import com.dingtalk.api.response.OapiV2DepartmentListsubidResponse;
import com.ly.dingtalk.common.exception.DingApiException;
import com.ly.dingtalk.component.application.IDingApp;
import com.ly.dingtalk.constant.Language;
import com.ly.dingtalk.service.AbstractDingNewApi;
import com.ly.dingtalk.service.dept.IDingDeptHandler;

import cn.hutool.core.util.NumberUtil;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;

/**
 * 钉钉部门
 *
 * @author wangzhen
 */
@Slf4j
public class DingDeptHandler extends AbstractDingNewApi implements IDingDeptHandler {

    public DingDeptHandler(@NonNull IDingApp app) {
        super(app);
    }

    @Override
    public OapiV2DepartmentGetResponse.DeptGetResponse getByDeptId(@NonNull Long deptId, @NonNull Language language) {
        OapiV2DepartmentGetRequest request = new OapiV2DepartmentGetRequest();
        request.setDeptId(deptId);
        request.setLanguage(language.name());
        final OapiV2DepartmentGetResponse response = execute(
                GET_DEPT_DETAIL,
                request,
                () -> "获取部门详情"
        );
        return response.getResult();
    }

    @Override
    public OapiV2DepartmentListsubidResponse.DeptListSubIdResponse getDeptIdList(@NonNull Long deptId) {
        OapiV2DepartmentListsubidRequest request = new OapiV2DepartmentListsubidRequest();
        request.setDeptId(deptId);
        OapiV2DepartmentListsubidResponse response = execute(
                GET_SUB_DEPT_ID_LIST,
                request,
                () -> "获取子部门ID列表"
        );
        return response.getResult();
    }

    @Override
    public List<OapiV2DepartmentListsubResponse.DeptBaseResponse> getDeptList() {
        return this.getDeptList(null, Language.zh_CN);
    }

    @Override
    public List<OapiV2DepartmentListsubResponse.DeptBaseResponse> getDeptList(Long deptId, @NotNull Language language) {
        OapiV2DepartmentListsubRequest request = new OapiV2DepartmentListsubRequest();
        request.setDeptId(deptId);
        request.setLanguage(language.name());
        final OapiV2DepartmentListsubResponse response = execute(
                GET_SUB_DEPT_LIST,
                request,
                () -> "获取部门列表"
        );
        return response.getResult();
    }

    @Override
    public OapiV2DepartmentListparentbydeptResponse.DeptListParentByDeptIdResponse getParentDeptListByDeptId(@NonNull Long deptId) {
        OapiV2DepartmentListparentbydeptRequest request = new OapiV2DepartmentListparentbydeptRequest();
        request.setDeptId(deptId);
        OapiV2DepartmentListparentbydeptResponse response = execute(
                GET_PARENT_DEPT_LIST_BY_DEPT_ID,
                request,
                () -> "获取指定部门的所有父部门列表"
        );
        return response.getResult();
    }

    @Override
    public OapiV2DepartmentListparentbyuserResponse.DeptListParentByUserResponse getParentDeptListByUserId(@NonNull String userId) {
        OapiV2DepartmentListparentbyuserRequest request = new OapiV2DepartmentListparentbyuserRequest();
        request.setUserid(userId);
        OapiV2DepartmentListparentbyuserResponse response = execute(
                GET_PARENT_DEPT_LIST_BY_USER_ID,
                request,
                () -> "获取指定用户的所有父部门列表");
        return response.getResult();
    }
    
    @Override
    public SearchDepartmentResponse searchDepartmentWithOptions(@NonNull String queryWord, Integer offset, Integer size) {
        return execute(com.aliyun.dingtalkcontact_1_0.Client.class, client->{
            SearchDepartmentResponse searchDepartmentResult = null;
            try {
                SearchDepartmentHeaders searchDepartmentHeaders = new SearchDepartmentHeaders();
                searchDepartmentHeaders.xAcsDingtalkAccessToken = accessToken();
                SearchDepartmentRequest searchDepartmentRequest = new SearchDepartmentRequest()
                        .setQueryWord(queryWord)
                        .setOffset(NumberUtil.nullToZero(offset))
                        .setSize(ObjectUtils.defaultIfNull(size, 10));
                searchDepartmentResult = client.searchDepartmentWithOptions(searchDepartmentRequest, searchDepartmentHeaders, new RuntimeOptions());
            } catch (Exception e) {
                log.error("搜索部门ID报错!", e);
                throw new DingApiException(e.getMessage());
            }
            return searchDepartmentResult;
        }, ()->"搜索部门ID");
    }
}
